package command;

import Exceptions.ColumnNotFoundException;
import Exceptions.SubselectReturnsMoreThan1RowException;

public class Operator {
	private String op;

	public Operator(String op) {
		this.op = op;
	}

	public Boolean eval(String value, String right) {

		if (op == "=")
			return value.equals(right);

		if (op == ">")
			if (value.compareTo(right) > 0)
				return true;
			else
				return false;

		if (op == "<")
			if (value.compareTo(right) < 0)
				return true;
			else
				return false;

		if (op == ">=")
			if (value.compareTo(right) >= 0)
				return true;
			else
				return false;

		if (op == "<=")
			if (value.compareTo(right) <= 0)
				return true;
			else
				return false;

		if (op == "LIKE") {
			return value.matches(right);
		}
		return false;
	}

	public Boolean eval(String value, String field, String[] header,
			String[][] table) throws SubselectReturnsMoreThan1RowException {
		if (op == "IN") {
			int i;

			if (table[0].length != 1) {
				throw new SubselectReturnsMoreThan1RowException();
			}

			for (int j = 0; j < table.length; j++) {
				if (table[j][0].equals(value))
					return true;
			}
			return false;
		} else
			return false;
	}

	public String toString() {
		return this.op;
	}
}
